package com.have.blog.picture.controller;

import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.models.Picture;
import com.have.blog.picture.dto.PictureUrlDto;
import com.have.blog.picture.service.PictureFeignApi;
import com.have.blog.picture.service.PictureService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RefreshScope
@RestController
@Api(value = "文件服务相关接口", tags = {"文件服务相关接口"})
@Slf4j
public class PictureController implements PictureFeignApi {
    @Autowired
    private PictureService pictureService;

    /**
     * 截图上传图片
     * @param file
     * @return
     */
    @ApiOperation(value = "截图上传", notes = "截图上传")
    public Wrapper<List<PictureUrlDto>> cropperPicture(@RequestParam("file") MultipartFile file) {
        ArrayList<MultipartFile> list = new ArrayList<>();
        list.add(file);
        return pictureService.cropperPicture(list);
    }

    /**
     * 批量获取图片
     * @return
     */
    @ApiOperation(value = "通过fileIds获取图片信息接口", notes = "获取图片信息接口")
    public Wrapper<Map<Integer, String>> getPicturesUrl(
            @ApiParam(name = "pictureUids", value = "图片uids", required = false) @RequestParam(name = "pictureUids", required = false) List<Integer> pictureUids) {
        log.info("获取图片信息: {}", pictureUids);
        return pictureService.getPictures(pictureUids);
    }

    /**
     * 获取当张图片
     * @param pictureUid
     * @return
     */
    @Override
    public Wrapper<String> getPictureUrl(Integer pictureUid) {
        log.info("获取图片:{}", pictureUid);
        return pictureService.getPicture(pictureUid);
    }

    /**
     * 多文件上传
     * 上传图片接口   传入 userId sysUserId ,有那个传哪个，记录是谁传的,
     * projectName 传入的项目名称如 base 默认是base
     * sortName 传入的模块名， 如 admin，user ,等，不在数据库中记录的是不会上传的
     *
     * @return
     */
    @ApiOperation(value = "多图片上传接口", notes = "多图片上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filedatas", value = "文件数据", required = true),
            @ApiImplicitParam(name = "userUid", value = "用户UID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "sysUserId", value = "管理员UID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "projectName", value = "项目名", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortName", value = "模块名", required = false, dataType = "String")
    })
    public Wrapper<List<Picture>> uploadPics(List<MultipartFile> filedatas) {
        // 获取系统配置文件
        Wrapper<List<Picture>> listWrapper = pictureService.batchUploadPicture(filedatas);
        return listWrapper;
    }
}
